package com.itmg.tools.timer.loader;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import com.itmg.cache.StaticInformationBean;
import com.itmg.dao.CacheDAO;
import com.itmg.dao.CategoryDAO;
import com.itmg.dao.model.CacheModel;
import com.itmg.dao.model.CategoryModel;
import com.itmg.factory.DAOFactory;
import com.itmg.tools.timer.KeywordsTimerTask;
import com.itmg.tools.timer.UpdateCacheTimerTask;

/**
 * 
 * @author Jason
 */
public class InitLoaderServlet extends HttpServlet {
	private static final long serialVersionUID = -3256701977456152302L;

	@Override
	public void init() throws ServletException {
		Timer timer = new Timer();

		// cache 定时器，cache一天清理一次		TimerTask cacheTask = new UpdateCacheTimerTask();
		timer.schedule(cacheTask, 1000, 24 * 3600 * 1000);
//		timer.schedule(cacheTask, 1000, 60 * 1000);
		
		TimerTask keywordtask = new KeywordsTimerTask();
		timer.schedule(keywordtask, 1000, 24 * 3600 * 1000);
		
		//启动的时候同步内存中数据库中的Md5
		CacheDAO cacheDAO = DAOFactory.getCacheDAO();
		List<CacheModel> cacheList = null;
		try {
			cacheList = cacheDAO.getProductMd5All();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		if(cacheList != null){
			for(CacheModel model : cacheList){
				StaticInformationBean.setPageTable(model.getProductMd5(), model.getPage_total());
				StaticInformationBean.setProductMd5(model.getProductMd5());
				StaticInformationBean.setTotal(model.getProductMd5(), model.getTotal());
			}
		}
		
		//启动的时候加载category
		CategoryDAO categoryDAO = DAOFactory.getCategoryDAO();
		List<CategoryModel> list = null;
		try {
			list = categoryDAO.getCategory();
		} catch (Exception e) {
			e.printStackTrace();
		}
		StaticInformationBean.setCategoryList(list);
		getServletContext().setAttribute("categoryList", list);
		

	}

}
